package com.pioneer.gui.invoice.view;

import com.pioneer.gui.invoice.view.InvoicePanel;
import com.pioneer.gui.invoice.controller.InvoiceSearchButtontoInvoiceSearchListAdapter;
import com.pioneer.gui.invoice.model.InvoiceModel;
import com.pioneer.gui.invoice.model.VectorList;
import com.pioneer.gui.invoice.model.DropDownList;
import com.pioneer.gui.invoice.util.Constants;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Dimension;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.util.Vector;

public class CustomerPanel
extends JPanel
implements Constants
{	
	private final int CUS_NAME_LENGTH=10;
	//private InvoiceModel model;
	
	public JTextField customerTextField;
	public JButton invoiceSearchButton;
	public JComboBox customerDropDown;
	
	public CustomerPanel(InvoiceModel model)
	{
		//this.model=model;
		this.setLayout(new GridLayout(2,1));
		
		JPanel quickSearchPanel=new JPanel(new BorderLayout());		
		customerTextField=new JTextField(CUS_NAME_LENGTH);
		customerTextField.addKeyListener(new CustomerTextFieldtoDropDownAdapter());
		quickSearchPanel.add(customerTextField,BorderLayout.WEST);
		
		invoiceSearchButton=new JButton("Find Invoices");
		invoiceSearchButton.addActionListener(new InvoiceSearchButtontoInvoiceSearchListAdapter(model));
		quickSearchPanel.add(invoiceSearchButton,BorderLayout.CENTER);
		
		this.add(new LabelPanel(new JLabel("Customer Quick Search"),quickSearchPanel));
		
		customerDropDown=new JComboBox(model.getCusId());
		customerDropDown.addActionListener(new CustomerDropDownAdapter());
		customerDropDown.setPreferredSize(new Dimension(5,1));
		customerDropDown.setSelectedIndex(0);
		
		this.add(new LabelPanel(new JLabel("List Of All Customers"),customerDropDown));
	}
	
	class CustomerDropDownAdapter
	implements ActionListener
	{
		CustomerDropDownAdapter()
		{
		}
		
		public void actionPerformed(ActionEvent e)
		{			
			/*
			DropDownList customerDropDown=(DropDownList)CustomerPanel.this.customerDropDown.getModel();
			if (customerDropDown.getSelectedItem()!=null)
				CustomerPanel.this.model.setName((customerDropDown.getSelectedItem().toString()));
			if (customerDropDown.getDatabaseKey()!=null)
			CustomerPanel.this.model.setCusId(((Integer)customerDropDown.getDatabaseKey()).intValue());
			*/
		}
	}
	
	class CustomerTextFieldtoDropDownAdapter
	extends KeyAdapter 
	{	
	
		public void keyReleased(KeyEvent e)
		{
			VectorList customerData=(VectorList)CustomerPanel.this.customerDropDown.getModel();			
			Vector list=customerData.getDropDownValues();
			int i=1;
			boolean found=false;
			while (!found&&i<list.size())
			{
				String curVectorValue=list.elementAt(i).toString();
				String curFieldValue=CustomerPanel.this.customerTextField.getText();
				if (curVectorValue.length()>=curFieldValue.length())
				{
					if (curFieldValue.equalsIgnoreCase(curVectorValue.substring(0,curFieldValue.length())))
					{
						found=true;
						//System.out.println("Found match at "+curVectorValue);
						if (curFieldValue.length()>0)
							CustomerPanel.this.customerDropDown.setSelectedIndex(i--);
						else
							CustomerPanel.this.customerDropDown.setSelectedIndex(0);
						CustomerPanel.this.customerDropDown.revalidate();
						CustomerPanel.this.customerDropDown.repaint();
					}
				}
				i++;				
			}
		}
	}
}

	
